//CC Dev.cpp
//CC Dev Main
/*
Nathan Currier / Rideliner
	nathan.currier@gmail.com
	http://rideliner.uni.cc
ColorCoded
	http://code.google.com/p/colorcoded/
*/

#include <iostream> //cin & cout
#include <fstream> //in/out-put to files
#include <iomanip> //setf & setprecision
#include <string> //string class
#include <sstream>
#include <cstdio>
#include <stdlib.h>
#include <windows.h>
#include <WinInet.h>
#include <tchar.h>
#include <urlmon.h>
#include "Version.h"
#include "../VERS_DECL.h"
#pragma comment(lib, "urlmon.lib")
#pragma warning(disable: 4996)
#include <ctype.h>
using namespace std;

int menu();
void change_vers(Version &change);
void separate(Version &setter, bool cc_vers = true);
void change_files(Version CC, Version Dev);

int main()
{
	ifstream update;
	fstream version;
	string update_chck, vers_new, dev_ver_new;
	string trans_cpp, trans_cpp2, vers_chck, vers_chck2;
	Version CC, Dev;
	char up_cont;
	int option;
	char cont_new_vers = 'y';

	update.open("Update.dat", ios::in);
	getline(update, update_chck);
	update.close();

	if(update_chck == "Update")
	{
		do
		{
			rename("ColorCoded.exe", "CC1.exe");
			HRESULT hr = URLDownloadToFile ( NULL, _T("http://colorcoded.googlecode.com/files/ColorCoded.exe"), _T("ColorCoded.exe"), 0, NULL );

			if(hr == 0)
			{
				remove("CC1.exe");
				cout << "Updated ColorCoded.exe";
				up_cont = 'n';
			}
			else
			{
				rename("CC1.exe", "ColorCoded.exe");
				cout << "Unable to update \'ColorCoded.exe\'.";
				cout << "\n\nWould you like to try to update again (Y/N): ";
				cin >> up_cont;

				up_cont = tolower(up_cont);

				system("cls");
			}
		}
		while(up_cont == 'y');

		remove("Update.dat");
		system("cmd.exe /c start \"\" \"ColorCoded.exe\"");
		return 0;		
	}

	separate(CC);
	separate(Dev, false);

	do
	{
		option = menu();
		
		if(option == 3) //Download Source
		{
			system("svn checkout http://colorcoded.googlecode.com/svn/trunk/ ./");
		}
		
		if(option == 2) //Update ColorCoded version
		{			
			cout << "ColorCoded:" << endl << endl;
			change_vers(CC);

			change_files(CC, Dev);
		}

		if(option == 1) //Update CC Dev version
		{
			cout << "CC Dev:" << endl << endl;
			change_vers(Dev);


			change_files(CC, Dev);
		}

		if(!(option >= 0 && option <= 3))
		{
			cout << "Invalid Option";
		}
		if(option != 0)
		{
			system("pause");
		}
	}
	while(option != 0);

	return 0;
}

int menu()
{
	system("cls");
	int option;
	cout << "Please choose an option:\n";
	cout << "3\tDownload Source" << endl;
	cout << "2\tUpdate ColorCoded version" << endl;
	cout << "1\tUpdate CC Dev version" << endl;
	cout << "0\tQuit" << endl;
	cin >> option;
	cin.ignore(80, '\n');

	system("cls");

	return option;
}

void change_vers(Version &change)
{
	bool type_up, release;
	
	cout << "1\tMajor Update" << endl;
	cout << "0\tMinor Update (Default)" << endl;
	cin >> type_up;

	cout << "\n1\tBeta" << endl;
	cout << "0\tRelease (Default)" << endl;
	cin >> release;
	
	if(type_up == true)
	{
		change.set_a((change.get_a() + 1));
		change.set_b(0);
	}
	else
	{
		change.set_b((change.get_b() + 1));
	}
	
	change.set_d((change.get_d() + 1));
	
	if(release == true)
	{
		change.set_c(1);
	}
	else
	{
		change.set_c(0);
	}
}

void separate(Version &setter, bool cc_vers)
{
	string _find = VERSION;
	if(!cc_vers)
	{
		_find = DEV_VER;
	}
	_find = ('\"' + _find + '\"');

	size_t first, second, third, fourth, fifth;
	string S_a, S_b, S_c, S_d; //String parts
	int V_a, V_b, V_c, V_d; //Version parts

	first = _find.find('\"');
	second = _find.find(',', (first + 1));
	third = _find.find(',', (second + 1));
	fourth = _find.find(',', (third + 1));
	fifth = _find.find('\"', (fourth + 1));

	S_a = _find.substr((first + 1), (second - first - 1));
	S_b = _find.substr((second + 1), (third - second - 1));
	S_c = _find.substr((third + 1), (fourth - third - 1));
	S_d = _find.substr((fourth + 1), (fifth - fourth - 1));

	V_a = atoi(S_a.c_str());
	V_b = atoi(S_b.c_str());
	V_c = atoi(S_c.c_str());
	V_d = atoi(S_d.c_str());

	setter.set(V_a, V_b, V_c, V_d);
}

void change_files(Version CC, Version Dev)
{
	ofstream changer;

	changer.open("VERS_DECL.h", ios::out | ios::trunc);

	changer << "//VERS_DECL.h" << endl;
	changer << "//Version Declarations" << endl;
	changer << "/*" << endl;
	changer << "Nathan Currier / Rideliner" << endl;
	changer << "	nathan.currier@gmail.com" << endl;
	changer << "	rideliner.uni.cc" << endl;
	changer << "ColorCoded" << endl;
	changer << "	http://code.google.com/p/colorcoded" << endl;
	changer << "*/" << endl << endl;
	changer << "#define VERSION							" << CC.get_vers(",", true) << endl;
	changer << "#define DEV_VER							" << Dev.get_vers(",", true) << endl;
	changer << "#define VERSION1						" << CC.get_vers(",") << endl;
	changer << "#define DEV_VER1						" << Dev.get_vers(",") << endl;
}
